package com.lly;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/servlet1")
public class Servlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");


        HttpSession session = req.getSession();
        //1.获取session,先判断是否有特殊cookie:sessionid
        //2.如果有,(1)没过期则获取session  (2)过期了重新创建
        //3.如果没有,则创建session

        //获取sessionid
        System.out.println(session.getId());
        //判断是否为创建的
        System.out.println(session.isNew());
        //在session保存数据
        session.setAttribute("username",username);

        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("<h1>成功</h1>");
    }
}
